/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.business;

/**
 *
 * @author david
 */
public class CategorisedTime implements Cloneable {

    private Double fixed;
    private Double semiFlexi;
    private Double flexi;

    public CategorisedTime() {
    }

    public Double getFixed() {
        return fixed;
    }

    public void setFixed(Double fixed) {
        this.fixed = fixed;
    }

    public Double getSemiFlexi() {
        return semiFlexi;
    }

    public void setSemiFlexi(Double semiFlexi) {
        this.semiFlexi = semiFlexi;
    }

    public Double getFlexi() {
        return flexi;
    }

    public void setFlexi(Double flexi) {
        this.flexi = flexi;
    }

    public Double getTotal() {
        if (fixed == null && semiFlexi == null && flexi == null) {
            return null;
        } else {
            return (fixed != null ? fixed : 0) +
                    (semiFlexi != null ? semiFlexi : 0) +
                    (flexi != null ? flexi : 0);
        }
    }

    /**
     * This method will subtract the number of holidays from the categorised
     * working time. In order, time is allocated from the supplied holiday to
     * fixed time, semi-flexi-time then flexi-time.
     *
     * @param holiday
     */
    public void subtractHoliday(Double holiday) {

        // If the holiday isn't even there then just abort
        if (holiday == null) {
            return;
        }

        // This implementation might look odd but it
        // does make sense and is very efficient.

        if (fixed != null) {
            fixed -= holiday;
            if (fixed >= 0.0) {
                return;
            }
            holiday = -fixed;
            fixed = 0.0;
        }

        if (semiFlexi != null) {
            semiFlexi -= holiday;
            if (semiFlexi >= 0.0) {
                return;
            }
            holiday = -semiFlexi;
            semiFlexi = 0.0;
        }

        if (flexi != null) {
            flexi -= holiday;
        }
    }

    public CategorisedTime clone() {
        CategorisedTime clonedObject = new CategorisedTime();
        clonedObject.setFixed(fixed);
        clonedObject.setSemiFlexi(semiFlexi);
        clonedObject.setFlexi(flexi);
        return clonedObject;
    }
}
